package com.li.onjava8.concurrent.cf.deadlock;

public class Philosopher implements Runnable{

    private final int seat;

    private final StickHolder left, right;

    /**
     * 确定位置，定位左右手筷子
     * @param seat
     * @param left
     * @param right
     */
    public Philosopher(int seat, StickHolder left, StickHolder right) {
        this.seat = seat;
        this.left = left;
        this.right = right;
    }

    @Override
    public String toString() {
        return "P" + seat;
    }

    /**
     * 拿筷子，吃饭
     */
    @Override
    public void run() {
        while (true) {
            System.out.println("Thinking");
            right.pickUp();
            left.pickUp();
            System.out.println(this + "eating");
            right.putDown();
            left.putDown();
        }

    }
}
